<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class IndexController extends BaseController
{
    protected $indexModel;

    public function __construct()
    {
        parent::__construct();
        $this->indexModel = new \app\api\model\IndexModel();
    }

    /**
     * 所有下拉选项的列表
     * @param string $method
     */
    public function options()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Index');
                $check = $validate->scene('options')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->indexModel->optionsList($this->userid,$this->departmentId,$data)); 
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 数据面板
     * 
     */
    public function board()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Index');
                $check = $validate->scene('board')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->indexModel->board($this->userid,$this->departmentId,$data['type']));    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 消耗数据列表
     * @param string $userid        用户userid
     * @param int    $brand_id      品牌id
     * @param int    $product_id    产品id
     * @param string $exact         视频名称
     * @param string $start         开始时间
     * @param string $end           结束时间
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Index');
                $check = $validate->scene('list')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->indexModel->getList($this->userid,$this->departmentId,$data));     
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . '，错误信息：'. $e->getMessage());
        }    
    }

    /**
     * 消耗数据下载
     * @param string $userid        用户userid
     * @param string $direct_userid 编导userid
     * @param string $camera_userid 摄像userid
     * @param int    $brand_id      品牌id
     * @param int    $product_id    产品id
     * @param string $exact         视频名称
     * @param string $start         开始时间
     * @param string $end           结束时间
     */
    public function export()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Index');
                $check = $validate->scene('list')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                $info = $this->indexModel->getExportData($this->userid,$this->departmentId,$data);
                if(empty($info)) return $this->jsonJump(ApiErrCode::NO_DOWNLOAD_DATA,ApiErrMsg::NO_DOWNLOAD_DATA);
                $title      = $this->indexModel->handle_download_title($data);
                $fieldsarr  = $this->indexModel->handle_download_fields($data);
                $excelTool  = \app\common\tool\Excel::getInstance();
                $excelTool->export_effect_data($info, $fieldsarr, $title);  
                return;
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . '，错误信息：'. $e->getMessage());
        }     
    }
}